package com.baizhi.realm;

import com.baizhi.dao.AdminDAO;
import com.baizhi.entity.Admin;
import org.apache.shiro.authc.*;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class AuthenRealm extends AuthenticatingRealm {

    @Autowired
    private AdminDAO adminDAO;

    /**
     *日志对象
     */
//    LoggerFactory.getLogger


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1、从令牌中获取用户名
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String username = token.getUsername();

        //2、通过DAO查询数据库，获取数据库中保存的数据库
//        adminDAO.selectByUsername(new QueryWrapper<Admin>().eq("username", username));
        Admin admin = adminDAO.selectByUsername(username);

//        对象不为null，封装info
        if (admin != null){
            /**
             * 参数1 数据库查到的账号
             * 参数2 数据库查到的密码
             * 参数3 当前对象的名字
             */
            this.getName();
            return new SimpleAuthenticationInfo(admin.getUsername(),
                    admin.getPassword(),
                    this.getName());

        }





        return null;
    }
}
